package com.jsm.tf.net.handler;

import com.jsm.tf.net.NettyClient;
import com.jsm.tf.net.message.request.HeartbeatMessage;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@ChannelHandler.Sharable
@Component
public class IdleStateEventHandler extends ChannelDuplexHandler {
    @Resource
    private NettyClient nettyClient;

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.WRITER_IDLE) {
                HeartbeatMessage heartbeatMessage = new HeartbeatMessage();
                log.debug("发送心跳消息");
                nettyClient.sendMsg(heartbeatMessage);
            }

        }
    }
}
